package com.qx.grainmall.product.service.impl;

import com.qx.common.vo.SkuEsModel;
import com.qx.grainmall.product.dao.*;
import com.qx.grainmall.product.entity.*;
import com.qx.grainmall.product.feign.EsFeignService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qx.common.utils.PageUtils;
import com.qx.common.utils.Query;

import com.qx.grainmall.product.service.SpuInfoService;


@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {

    @Autowired
    private SkuInfoDao skuInfoDao;

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private ProductAttrValueDao productAttrValueDao;

    @Autowired
    private EsFeignService esFeignService;

    @Autowired
    private SpuInfoDao spuInfoDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuInfoEntity> page = this.page(
                new Query<SpuInfoEntity>().getPage(params),
                new QueryWrapper<SpuInfoEntity>()
        );

        return new PageUtils(page);
    }

    //完成上架
    @Override
    public boolean up(Long spuId) throws IOException {
        //1.查出当前spuId 对应所有的 sku信息,品买信息还有名字
        List<SkuInfoEntity> skuInfoEntities = skuInfoDao.selectList(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
        //查spu对应的规格和属性信息
        List<ProductAttrValueEntity> attrlist = productAttrValueDao.selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
        //List<SkuEsModel.attr> attrs //规格参数属性列表
        List<SkuEsModel.Attr> attrs = attrlist.stream().map(aaa -> {
            SkuEsModel.Attr attr = new SkuEsModel.Attr();
            BeanUtils.copyProperties(aaa, attr);
            return attr;
        }).collect(Collectors.toList());

        //最终获取到需要上架的 list
        List<SkuEsModel> list = skuInfoEntities.stream().map(e -> {
            SkuEsModel skuEsModel = new SkuEsModel();
            BeanUtils.copyProperties(e, skuEsModel);
            //skuprice
            skuEsModel.setSkuPrice(e.getPrice());
            skuEsModel.setSkuImg(e.getSkuDefaultImg());
            skuEsModel.setHotScore(0L);
            //品牌名称
            BrandEntity brandEntity = brandDao.selectById(e.getBrandId());
            skuEsModel.setBrandName(brandEntity.getName());
            skuEsModel.setBrandImg(brandEntity.getLogo()); //品牌图片
            //分类
            CategoryEntity categoryEntity = categoryDao.selectById(e.getCatalogId());
            skuEsModel.setCatalogName(categoryEntity.getName());

            //规格参数列表
            skuEsModel.setAttrs(attrs);
            return skuEsModel;
        }).collect(Collectors.toList());
        Boolean up = esFeignService.up(list);
        if(up){
            return true;
        }
        return false;
    }

}